package uploadServer;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;

public class UploadImg {
	
	private static BufferedImage[] buffImage;
//	public static String path = "file://localhost/C:/QLNS-Img/";
	public static String path = "src/imageUpload/";
	public static String typeImg = "png";
	public static String fn;
	public static int imgWidth = 115;
	public static int imgHeight = 150;
	
	public static boolean upload(ImageInputStream iis, String fileName){
		boolean flag;
		Iterator<ImageReader> iteratorReader;
		ImageReader reader;
		File file;
		ImageOutputStream ios;
		Iterator<ImageWriter> iteratorWriter;
		ImageWriter writer;
		try {
			iteratorReader = ImageIO.getImageReaders(iis);
			reader = iteratorReader.next();
			reader.setInput(iis);
			
			file = new File(path+fileName+"."+typeImg);
			while(true){
				if(file.exists()){
					fileName = fileName+"_1";
					file = new File(path+fileName+"."+typeImg);
				}else break;
			}
			fn = fileName;
			ios = ImageIO.createImageOutputStream(file);
			iteratorWriter = ImageIO.getImageWritersByFormatName(typeImg);
			writer = iteratorWriter.next();
			writer.setOutput(ios);
			int count = reader.getNumImages(true);
			buffImage = new BufferedImage[count];
	
			for (int i = 0; i < count; i++){
				buffImage[i] = reader.read(i);
				writer.write(new IIOImage(buffImage[i], null, null));
			}
			iis.close();
			ios.close();
			BufferedImage originalImage = ImageIO.read(file);
			BufferedImage resizeImageJpg = createResizedCopy(originalImage, imgWidth, imgHeight, true);
			ImageIO.write(resizeImageJpg, typeImg, file); 
			
			flag = true;
		} catch (IOException e) {
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}
	
	public static boolean delete(String fileName){
		File fileDel = new File(path+fileName+"."+typeImg);
		if (!fileDel.exists()) return true;
		return fileDel.delete();
	}
	
	private static BufferedImage createResizedCopy(Image originalImage, int scaledWidth, int scaledHeight, boolean preserveAlpha){
		Image temp = originalImage.getScaledInstance(scaledWidth, scaledHeight,  Image.SCALE_SMOOTH);
	    int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
	    BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
	    Graphics2D g = scaledBI.createGraphics();
	    if (preserveAlpha) {
	    	g.setComposite(AlphaComposite.Src);
	    }
	    g.drawImage(temp, 0, 0, null); 
	    g.dispose();
	    return scaledBI;
	}
	
//	public static void main(String args[]) throws IOException{
//		File file = new File("C:/Documents and Settings/Windows XP/Desktop/IMG_2732.jpg");
//		InputStream is = new FileInputStream(file);
//		ImageInputStream iis = ImageIO.createImageInputStream(is);
//		boolean flag = UploadImg.upload(iis, "Anhnta");
//		System.out.println(flag);
//		System.out.println(fn);
////		delete(fn);
//	}
}
